import type { RouteRecordStringComponent, TreeNode } from '@vben/types';

import { requestClient } from '#/api/request';

export namespace MenuApi {
  export interface PageFetchParams {
    /** 页码 */
    page: number;
    /** 每页数量 */
    pageSize: number;
    /** 权限ID或父权限ID */
    id?: number;
  }

  export interface SaveParams {
    id?: number;
    parentId: number;
    component: string;
    name: string;
    path: string;
    icon: string;
    title: string;
    sort: number;
    description: string;
    code: string;
    menuType: 'function' | 'group' | 'lowcode';
    roles: string;
  }
}

/**
 * 获取所有菜单
 */
export async function getAllMenusApi() {
  return requestClient.get<RouteRecordStringComponent[]>('/auth/menu/all');
}

/**
 * 获取菜单列表
 */
export function getMenuListApi(params: MenuApi.PageFetchParams) {
  // 确保 id 作为数字传递
  const processedParams = {
    ...params,
    ...(params.id !== undefined && { id: Number(params.id) }),
  };

  // 直接传递参数，不要嵌套在 params 对象中
  return requestClient.post('/auth/permission/list', processedParams);
}

/**
 * 保存菜单
 */
export function saveMenuApi(data: MenuApi.SaveParams) {
  // 确保数字类型字段正确传递
  const processedData = {
    ...data,
    ...(data.id !== undefined && { id: Number(data.id) }),
    parentId: Number(data.parentId),
    sort: Number(data.sort),
  };

  return requestClient.post('/auth/permission/save', processedData);
}

/**
 * 删除菜单
 */
export function deleteMenuApi(id: number) {
  const numericId = Number(id);
  return requestClient.delete(`/auth/permission/delete?id=${numericId}`);
}

export interface MenuData {
  id?: number;
  parentId: number;
  title: string;
  name: string;
  component: string;
  path: string;
  icon: string;
  code: string;
  sort: number;
  redirect: string;
  description: string;
}

export async function getMenuTreeApi() {
  return requestClient.get<TreeNode[]>('/auth/permission/tree');
}

export async function getMenuDetailApi(id: number) {
  const numericId = Number(id);
  return requestClient.get<MenuData>(`/auth/permission/${numericId}`);
}

export async function deleteMenuById(id: number) {
  const numericId = Number(id);
  return requestClient.delete(`/auth/permission/delete?id=${numericId}`);
}

export async function getMenuRolesApi(userId: string) {
  return requestClient.get<string[]>(`/auth/permission/${userId}/roles`);
}

export interface DragDropParams {
  dragId: number;
  dropId: number;
  dropPosition: number;
  dropType: 'same' | 'sub';
}

/**
 * 更新菜单节点位置（拖放）
 */
export function updateMenuPositionApi(params: DragDropParams) {
  // 确保数字类型字段正确传递
  const processedParams = {
    ...params,
    dragId: Number(params.dragId),
    dropId: Number(params.dropId),
    dropPosition: Number(params.dropPosition),
  };

  return requestClient.post('/auth/permission/move', processedParams);
}

/**
 * 批量更新菜单排序
 */
export function updateMenuOrderApi(data: { id: number; order: number }[]) {
  // 确保数组中的数字类型字段正确传递
  const processedData = data.map(item => ({
    id: Number(item.id),
    order: Number(item.order),
  }));

  return requestClient.post('/auth/permission/updateOrder', processedData);
}
